package 链表;

public class 反转链表 {

    public static void main(String[] args) {
        反转链表 test = new 反转链表();
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(4);
        ListNode n5 = new ListNode(5);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        test.reverseList(n1);
    }

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }

        ListNode pre = head;
        ListNode node = head.next;
        ListNode next = null;
        pre.next = null;
        while (node.next != null) {
            next = node.next;
            node.next = pre;
            pre = node;
            node = next;
        }
        node.next = pre;

        return node;


    }


    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}
